package com.hutao.manage.util;
import java.io.IOException;

import com.aliyun.com.viapi.FileUtils;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.facebody.model.v20191230.CompareFaceRequest;
import com.aliyuncs.facebody.model.v20191230.CompareFaceResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

public class FaceDemo {
    
    
    private static final String ak_id = "LTAI4GBczAAVrCEV7n176k9w";
    private static final String ak_secret = "BCaRlCZBB6ySnc4PrLm1wrtJwxQhNX";

    /**
     * 图片要求:
     * 请求格式：JPEG、JPG、PNG、BMP。
     * 图像大小：图像大小不超过3M。
     * 图像分辨率：图片大小要求5x5像素以上，人脸的尺寸建议大于64x64像素。
     * @param imageURLA  图片A
     * @param imageURLB  图片B
     * @return
     */
    public static boolean faceContrast(String imageURLA,String imageURLB){
        boolean flag = false;
        
		//目前区域只开通了华东2(上海)
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", ak_id, ak_secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CompareFaceRequest request = new CompareFaceRequest();
        request.setRegionId("cn-shanghai");
        request.setImageURLA(imageURLA);
        request.setImageURLB(imageURLB);
        try {
            CompareFaceResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            Float confidence = response.getData().getConfidence();
            if (confidence > 61){
                flag = true;
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("人脸对比ErrCode:" + e.getErrCode());
            System.out.println("人脸对比ErrMsg:" + e.getErrMsg());
            System.out.println("人脸对比RequestId:" + e.getRequestId());
        }
        return flag;
    }
    
    public static String getUploadFile(String imgUrl) throws ClientException, IOException {
        String accessKey = ak_id;
        String accessKeySecret = ak_secret;
        //String file = "D://VerifyFaceMaskA.jpg";//  或者本地上传
       // String file = "https://fuss10.elemecdn.com/5/32/c17416d77817f2507d7fbdf15ef22jpeg.jpeg";
        FileUtils fileUtils = FileUtils.getInstance(accessKey,accessKeySecret);
        String ossurl = fileUtils.upload(imgUrl);
        System.out.println(ossurl);
        return ossurl;
    }

    public static void main(String[] args) throws ClientException, IOException {
        //String imageURLA = "https://chaojiguoke.oss-cn-shanghai.aliyuncs.com/img/894883263a674eceabe1f147610579fb.jpg";
        //String imageURLB = "https://chaojiguoke.oss-cn-shanghai.aliyuncs.com/img/17d824985ca048a5835b94b306ffb8a0.jpg";
    	String imageURLA = getUploadFile("D://8885.jpg");
    	String imageURLB = getUploadFile("D://8886.jpg");
        boolean b = faceContrast(imageURLA, imageURLB);
        if (b){
            System.out.println("通过:超过61%");
        }else{
            System.out.println("失败:低于61%");
        }
    }
}